package com.youkeda.test.j3.c9;

import com.alibaba.fastjson.JSON;
import com.youkeda.test.j3.c9.model.Weather;
import okhttp3.Call;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

import java.io.IOException;

/**
 * @author xshuai
 */
public class PageOperator {

    public static void main(String[] args) {
        String s = getContent("http://www.weather.com.cn/data/sk/101040200.html");
        Weather weather = getWeather(s);
        System.out.println("city=" + weather.getWeatherInfo().getCity());
    }

    public static String getContent(String url) {
        String result = "";
        OkHttpClient okHttpClient = new OkHttpClient();
        Request request = new Request.Builder()
                .url(url)
                .build();
        Call call = okHttpClient.newCall(request);
        try {
            Response response = call.execute();
            result = response.body().string();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

    public static Weather getWeather(String stringJson) {
        return JSON.parseObject(stringJson, Weather.class);
    }
}
